const { InsuranceType } = require("../models/insuranceTypeModel");

//保险分类新增
async function addInsurances(req,res){
    const { name,data=[]} = req.body;
    await InsuranceType.create({name,insurancesId:data});
    res.send({
        status:1,
        msg:'新增成功'
    })
};
//查看所有保险分类
async function lgetAllInsurances(req,res){
    const data =  await InsuranceType
    .find()
    .populate({
        path:'insurancesId'
    });
    if(data){
        res.send({
            status:1,
            msg:'获取成功',
            data,
        })
    }else{
        res.send({
            status:0,
            msg:'获取失败'
        })
    }
};
//获取单个id
async function getUpInsurances(req,res){
    const {_id} = req.query;
    const data = await InsuranceType.find({_id});
    if(data){
        res.send({
            status:1,
            msg:'获取成功',
            data,
        })
    }else{
        res.send({
            status:0,
            msg:'获取失败',
        })
    }
};
//修改
async function sureUpInsurance(req,res){
    const {_id,name} = req.body;
    const data =  await InsuranceType.updateOne({_id},{name});
    if(data.nModified){
        res.send({
           status:1,
           msg:'修改成功'
        })
    }else{
        res.send({
           status:0,
           msg:'修改失败',
        })
    }
};
//删除
async function deleteInsurances(req,res){
    const {_id} = req.body;
    console.log(_id);
    const data = await InsuranceType.findByIdAndDelete(_id);
    if(data){
        res.send({
            status:1,
            msg:'删除成功'
        })
    }else{
        res.send({
            status:0,
            msg:'删除失败'
        })
    }

}

module.exports = {addInsurances,lgetAllInsurances,getUpInsurances,sureUpInsurance,deleteInsurances}